-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Host function generic_hash()
with blake2b/blake3 support.
#4411
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Will be used to introduce hashing method for runtime.
Placed in new `crypto` module under `runtime`.
Will be used to switch between different hashing algorithms.
Note: no gas charging was introduced yet.
Inspired by `FunctionIndex::Blake2b`, which is also not researched in terms of execution cost.
Note: new module `crypto` was introduced.
Closed in favor of #4412, that targets |
Related Issue (created to take the work forward) #4892 |
casperlabs-bors-ng bot
added a commit
that referenced
this pull request
Oct 15, 2024
4903: Implement generic_hash() host function to support hashing algorithms r=igor-casper a=igor-casper This PR directly addresses #4892, referencing #4411 as prior work. Adds host function called generic_hash(input, type) with support for the following types: - HashAlgorithm::Blake2b - using the existing blake2b implementation, - HashAlgorithm::Blake3 - introducing the blake3 library, - HashAlgorithm::Sha256 - introducing the sha2 library, **Example usage** ```rs #![no_std] #![no_main] extern crate alloc; use alloc::string::String; use casper_contract::contract_api::cryptography; use casper_types::crypto::HashAlgorithm; #[no_mangle] pub extern "C" fn call() { let data = "sha256 hash test"; let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA]; let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256); assert_eq!( hash, expected, "Hash mismatch" ); } ``` **Notes** - Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside - The costs were referenced from #4411 Co-authored-by: igor-casper <[email protected]> Co-authored-by: igor-casper <[email protected]>
casperlabs-bors-ng bot
added a commit
that referenced
this pull request
Oct 15, 2024
4903: Implement generic_hash() host function to support hashing algorithms r=igor-casper a=igor-casper This PR directly addresses #4892, referencing #4411 as prior work. Adds host function called generic_hash(input, type) with support for the following types: - HashAlgorithm::Blake2b - using the existing blake2b implementation, - HashAlgorithm::Blake3 - introducing the blake3 library, - HashAlgorithm::Sha256 - introducing the sha2 library, **Example usage** ```rs #![no_std] #![no_main] extern crate alloc; use alloc::string::String; use casper_contract::contract_api::cryptography; use casper_types::crypto::HashAlgorithm; #[no_mangle] pub extern "C" fn call() { let data = "sha256 hash test"; let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA]; let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256); assert_eq!( hash, expected, "Hash mismatch" ); } ``` **Notes** - Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside - The costs were referenced from #4411 Co-authored-by: igor-casper <[email protected]> Co-authored-by: igor-casper <[email protected]>
casperlabs-bors-ng bot
added a commit
that referenced
this pull request
Oct 18, 2024
4903: Implement generic_hash() host function to support hashing algorithms r=EdHastingsCasperAssociation a=igor-casper This PR directly addresses #4892, referencing #4411 as prior work. Adds host function called generic_hash(input, type) with support for the following types: - HashAlgorithm::Blake2b - using the existing blake2b implementation, - HashAlgorithm::Blake3 - introducing the blake3 library, - HashAlgorithm::Sha256 - introducing the sha2 library, **Example usage** ```rs #![no_std] #![no_main] extern crate alloc; use alloc::string::String; use casper_contract::contract_api::cryptography; use casper_types::crypto::HashAlgorithm; #[no_mangle] pub extern "C" fn call() { let data = "sha256 hash test"; let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA]; let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256); assert_eq!( hash, expected, "Hash mismatch" ); } ``` **Notes** - Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside - The costs were referenced from #4411 Co-authored-by: igor-casper <[email protected]> Co-authored-by: igor-casper <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In order to support Zero Knowledge software, cryptography related utils should be implemented as a host functions, otherwise execution cost would be too unacceptable. For example running Risc0 VM proof verification for simple sum of squares takes more than 6000 CSPR. The underlying hash used: sha256.
This PR adds host function called
generic_hash(input, type)
with support for the following types:HashAlgoType::Blake2b
- used existing blake2b implementation,HashAlgoType::Blake3b
- blake3 library was introduced.We expect to add more algorithms, i.e. sha256, keccak, poseidon.
Example usage
Smart contract:
Node output:
Considerations
1. AssemblyScript support
Is AssemblyScript binding required? I am pretty sure no one uses it.
2. Gas cost
For now, I used the same cost for
generic_hash()
as defined forblake2()
- default fixed value 200. I can do benchmarks for WASM bytecode execution, but is there any conversion ratio between CPU time and execution cost?3. Place for core hashes implementation
Blake2b is implemented in
types
package, which does not seem to be reasonable. I added blake3 to new modulecrypto
in execution engine, but I am not fully sure if this is the correct place.4. Single vs multiple host function
Do we want to introduce separate host functions for each hash algorithm?
5. Place for
generic_hash()
in contract APII was thinking of using
runtime
, but finally decided to create newcrypto
module.